package com.example.industry.service.impl;

import com.example.industry.dao.ProjectDao;
import com.example.industry.dao.UserDao;
import com.example.industry.entity.Project;
import com.example.industry.entity.User;
import com.example.industry.enums.Roles;
import com.example.industry.service.ProjectService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService
{
    @Resource
    private ProjectDao projectDao;

    @Resource
    private UserDao userDao;

    @Override
    public boolean createProject(String projectName, Integer chairmanId, String projectIntroduction)
    {
        Project newProject = new Project();
        newProject.setProjectName(projectName);
        newProject.setChairmanId(chairmanId);
        newProject.setProjectIntroduction(projectIntroduction);
        return projectDao.addProject(newProject);
    }

    @Override
    public boolean deleteProjectById(Integer projectId)
    {
        return projectDao.deleteById(projectId);
    }

    @Override
    public boolean updateProject(Integer projectId, String projectName, String projectIntroduction)
    {
        Project project = projectDao.findById(projectId);
        project.setProjectName(projectName);
        project.setProjectIntroduction(projectIntroduction);
        return projectDao.update(project);
    }

    @Override
    public List<User> findUsersByProjectIdAndRole(Integer projectId, Roles role)
    {
        List<User> userList = new ArrayList<>();
        try
        {
            List<Integer> result = projectDao.findMembersByProjectId(projectId, role);
            if (result == null) return null;
            for (Integer user_id : result)
            {
                User user = userDao.findById(user_id);
                userList.add(user);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public boolean updateUserRoleInProject(Integer projectId, Integer userId, Roles role)
    {
        return projectDao.updateUserRoleInProject(projectId, userId, role);
    }

    @Override
    public boolean deleteUserInProjectByUserId(Integer projectId, Integer userId)
    {
        return projectDao.deleteMember(projectId, userId);
    }

}
